/**
 * Package:com.goldcitynet.engine.api.as3.service;
 * $Id$
 * Copyright(c) 2001-2005 www.afteryuan.com
 */
package com.afteryuan.engine.api.as3.service;

import com.afteryuan.engine.model.CartoonData;
import com.afteryuan.engine.model.Cartoon;
import com.afteryuan.engine.model.UserCartoon;
import com.afteryuan.engine.service.CartoonDataService;
import com.afteryuan.engine.service.CartoonService;
import com.afteryuan.engine.service.UserCartoonService;
import com.afteryuan.engine.api.as3.dto.As3CartoonData;
import com.afteryuan.engine.api.as3.dto.As3Cartoon;
import com.afteryuan.engine.api.as3.MessagePush;

import java.util.*;
import java.lang.reflect.InvocationTargetException;

import org.apache.commons.logging.LogFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.beanutils.BeanUtils;


/**
 * 用As3 RPC方式提供的API,用于动画人物的换装时候的数据交互
 * <p/>
 * <p><a href="CartoonDataFlexService.java.html"><i>View Source</i></a></p>
 *
 * @author <a href="mailto:afteryuan@afteryuan.com">$Author$</a>
 * @version $Revision$
 */
public class CartoonFlexService extends AbstractFlexService {
    private static Log log = LogFactory.getLog(CartoonFlexService.class);
    private CartoonDataService cartoonDataService;
    private CartoonService cartoonService;
    private UserCartoonService userCartoonService;

    public void setUserCartoonService(UserCartoonService userCartoonService) {
        this.userCartoonService = userCartoonService;
    }

    public void setCartoonDataService(CartoonDataService cartoonDataService) {
        this.cartoonDataService = cartoonDataService;
    }

    public void setCartoonService(CartoonService cartoonService) {
        this.cartoonService = cartoonService;
    }

    /**
     * 保存CartoonData列表
     * @param cartoonDatas
     */
    public void saveCartoonDataList(List<As3CartoonData> cartoonDatas){
        log.debug("input:"+cartoonDatas);
        if(cartoonDatas==null)throw new NullPointerException("cartoonData list is null");
        String cartoonId = cartoonDatas.get(0).getCartoonId();
        for (As3CartoonData cartoonData : cartoonDatas) {
            saveCartoonData(cartoonData);
        }
        Map headers = new Hashtable();
        headers.put("cartoonId",cartoonId);
        MessagePush.pushSystemMessge(headers,null);
    }

    /**
     * 保存CartoonData
     * @param cartoonData
     */
    public void saveCartoonData(As3CartoonData cartoonData){
        log.debug("input:"+cartoonData);
        cartoonDataService.save(convert(cartoonData));
    }

    /**
     * Cartoon
     * @param cartoon
     */
    public As3Cartoon saveCartoon(As3Cartoon cartoon){
        log.debug("input:"+cartoon);
        Cartoon c = convert(cartoon);
        cartoonService.save(c);
        return convert(c);
    }

    /**
     * 保存UserCartoon
     *
     * @param cartoonId
     * @param userId
     * @return  UserCartoon的Id
     */
    public String saveUserCartoon(String cartoonId,String userId){
        log.debug("input:cartoonId:"+cartoonId+" userId:"+userId);
        UserCartoon c = new UserCartoon(userId,cartoonId);
        cartoonService.save(c);
        return c.getId();
    }

    /**
     *
     * @param id
     * @return
     */
    public List<As3CartoonData> listByCartoonId(String id){
        List<CartoonData> cartoonDatas = cartoonDataService.listByCartoon(id);
        if(cartoonDatas==null)return null;
        List<As3CartoonData> datas= new ArrayList<As3CartoonData>();
        for (CartoonData cartoonData : cartoonDatas) {
            datas.add(convert(cartoonData));
        }
        return datas;
    }

    /**
     * 取得 Cartoon
     * @param id
     * @return
     */
    public As3Cartoon getCartoon(String id){
        return convert(cartoonService.get(id));
    }

    /**
     * 取得 Cartoon
     * @param userId
     * @return
     */
    public As3Cartoon getCartoonByUser(String userId){
        log.debug("input:"+userId);
        UserCartoon u = userCartoonService.getByUserId(userId);
        if(u==null) return null;
        Cartoon c = cartoonService.get(u.getCartoonId());
        if(c == null) return null;
        return convert(c);
    }

    /**
     * 修改 Cartoon
     * @param cartoon
     * @return
     */
    public void updateCartoon(As3Cartoon cartoon){
        if(cartoon.getId()==null || cartoon.getId().length()!=32)throw new IllegalArgumentException("legal id is need");
        Cartoon c = cartoonService.get(cartoon.getId());
        try {
            BeanUtils.copyProperties(c,cartoon);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        cartoonService.save(c);
    }

    private Cartoon convert(As3Cartoon as3Cartoon){
        Cartoon cartoon = new Cartoon();
        try {
            BeanUtils.copyProperties(cartoon,as3Cartoon);
            return cartoon;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    private As3Cartoon convert(Cartoon cartoon){
        As3Cartoon as3Cartoon = new As3Cartoon();
        try {
            BeanUtils.copyProperties(as3Cartoon,cartoon);
            return as3Cartoon;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    private CartoonData convert(As3CartoonData as3CartoonData){
        CartoonData cartoon = new CartoonData();
        try {
            BeanUtils.copyProperties(cartoon,as3CartoonData);
            cartoon.setCartoon(cartoonService.get(as3CartoonData.getCartoonId()));
            return cartoon;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }

    private As3CartoonData convert(CartoonData cartoonData){
        As3CartoonData as3CartoonData = new As3CartoonData();
        try {
            BeanUtils.copyProperties(as3CartoonData,cartoonData);
            return as3CartoonData;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return null;
    }
}